Thread: [Help]Or statement

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    5

    [Help]Or statement

    How to get a program like this work?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    int a,b,c;
    -1<a<10 or a==50;
    -2<b<11 or b==51;
    c=a+b;
    
    do{
    a=rand();
    b=rand();
    printf("%d,%d,%d",a,b,c);
    }while(1==1);
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Mikri Maus View Post
    How to get a program like this work?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    int a,b,c;
    -1<a<10 or a==50;
    -2<b<11 or b==51;
    c=a+b;
    
    do{
    a=rand();
    b=rand();
    printf("%d,%d,%d",a,b,c);
    }while(1==1);
    }
    Well, at the very least you'll need to use valid C code. A better description of the problem would be useful too...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Welcome to the forum.

    Where are the if statements??
    This section
    Code:
    -1<a<10 or a==50;
    -2<b<11 or b==51;
    c=a+b;
    should be like
    Code:
    if( ( -1 < a && a < 10 ) || a == 50)
    {
        do_something;
    }
    You need to do the same with the second if statement.
    The "||" operator is the OR and the "&&" operator, the AND.
    You see, you want your condition to be true if, a is larger than -1 (-1 < a) AND less than 10 (a < 10) OR a to be equal with 50 (a == 50).

    You need to do this in order to take random numbers
    Code:
    srand( (unsigned int)time ( NULL ) );
    You should check your indent style as well!

    However, you are trying to apply your logic (the if statements), before giving variables a value!! As a result, you need to move your logic, in order o be executed after you give your variables values!

    You have a loop that will be executed as long as 1 == 1, which means forever. Not good. That is an infinite loop and will cause your program to run forever!

    Let's see another example:
    Assume that I want to have a loop, that will be executed again and again, as long as, the variable named "a" has a value less than 5 or equal to 6. Initial variable of "a" is zero. Every time the loop is executed, add 2 to variable "a". Print the final value of "a".
    Here is an example solution with comments.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    /* void states that we receive no arguments from cmd */
    int main(void)
    {
        /* Initial value for a. */
        int a = 0;
        
        /* We know that our loop is going to be executed at least once,
           so a do-while loop is ok.
         */
        do{
            /* Every time the loop gets executed, add 2 to a. */
            a = a + 2;
            
            /* Conitnue looping, while a is less than 5 OR a is equal to 6*/
        }while(a < 5 || a == 6);
        
        /* Print value of a.*/
        printf("a = %d\n", a);
        
        /* Exit the program. */
        return 0;
    }
    I suggest you run this code in a paper with a pen and see what the value of a will be. Then run the code to test.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Registered User
    Join Date
    Oct 2013
    Posts
    5
    Thanks. I understand it now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While statement
    By Mentallic in forum C Programming
    Replies: 5
    Last Post: 10-12-2012, 06:26 AM
  2. If statement hw help
    By AlexTank853 in forum C Programming
    Replies: 10
    Last Post: 09-22-2012, 01:28 PM
  3. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 03:18 PM
  4. Help with if statement
    By nafix in forum C Programming
    Replies: 5
    Last Post: 09-14-2007, 05:49 PM
  5. If Statement
    By boontune in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2003, 07:56 AM